package cn.ad.server.web.spring.service.actions.targeturl;

import cn.ad.server.web.spring.service.actions.command.TrackCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Appsflyer平台
 * 以大括号定义的宏。
 * click url:
 * http://app.appsflyer.com/
 * app_id={app_id}&pid={fmobi}&clickid={guid}&android_id={android_id}
 * &advertising_id={advertising_id}&idfa={idfa}&sid={sid}
 * &aid={aid}&cid={cid}&mid={mid}&ch={ch}&os={os}
 * 
 * @author wangwei
 *
 */
public class AppsflyerConvertTargetUrl implements IConvertTargetUrl{
	
	private static Logger LOGGER = LoggerFactory.getLogger(AppsflyerConvertTargetUrl.class);
	

	public String convertTargetUrl(String targetUrl, TrackCommand command){
		LOGGER.info("Appsflyer 激活回调 信息。targetUrl{}, TrackCommand{}",targetUrl, command.toString());
		return targetUrl.replaceAll(" ", "")
				.replaceAll("\\{guid\\}", null2Empty(command.getGuid()))
				.replaceAll("\\{android_id\\}", null2Empty(command.getAndroidid()))
				.replaceAll("\\{advertising_id\\}", null2Empty(command.getAdvertising_id()))
				.replaceAll("\\{idfa\\}", null2Empty(command.getIdfa()))
				.replaceAll("\\{sid\\}", String.valueOf(command.getSid()))
				.replaceAll("\\{aid\\}", String.valueOf(command.getAid()))
				.replaceAll("\\{cid\\}", String.valueOf(command.getCid()))
				.replaceAll("\\{mid\\}", String.valueOf(command.getMid()))
				.replaceAll("\\{ch\\}", null2Empty(command.getCh()))
				.replaceAll("\\{os\\}", command.getOsName());
	}
	
	private String null2Empty(String targetUrl){
		if(targetUrl==null){
			targetUrl = "";
		}
		return targetUrl;
	}

}
